/* 
 * File:   DegreeParameterCommand.cpp
 * Author: Gab'z
 * 
 * Created on 5 juin 2012, 12:27
 */

#include "DegreeParameterCommand.h"
#include "model/Context.h"
#include "model/memento/AngleUnitParameterMemento.h"

namespace calculator {

    DegreeParameterCommand::DegreeParameterCommand(const std::string name) : ParametersCommand(name) {
    }

    DegreeParameterCommand::DegreeParameterCommand(const DegreeParameterCommand& orig) : ParametersCommand(orig._name) {
    }

    DegreeParameterCommand::~DegreeParameterCommand() {
    }

    DegreeParameterCommand* DegreeParameterCommand:: clone() const {
        return new DegreeParameterCommand(_name);
    }

    void DegreeParameterCommand::apply(Parameters* parameters) const {
        parameters->setAngleUnit(Parameters::DEGREE);
    }
    
    const Memento* DegreeParameterCommand::createMemento() const throw (CommandException) {
        return new AngleUnitParameterMemento(clone(), Context::getInstance()->getParameters()->getAngleUnit());
    }

    void DegreeParameterCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const AngleUnitParameterMemento* angleUnitParameterMemento = dynamic_cast<const AngleUnitParameterMemento*> (memento);
        if (angleUnitParameterMemento == 0) {
            std::string str = "Bad Memento type, expected : AngleUnitParameterMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getParameters()->setAngleUnit(angleUnitParameterMemento->_angleUnit);
    }
}